package hn.cch.spring.aop.proxy.proxy;


import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;

public class MethodInterceptorImpl implements MethodInterceptor {

    public DynamicProxyApi dynamicProxyApi;

    public MethodInterceptorImpl() {
    }

    public MethodInterceptorImpl(DynamicProxyApi dynamicProxyApi) {
        this.dynamicProxyApi = dynamicProxyApi;
    }

    public DynamicProxyApi getProxyInterface() {
        return dynamicProxyApi;
    }

    public void setProxyInterface(DynamicProxyApi dynamicProxyApi) {
        this.dynamicProxyApi = dynamicProxyApi;
    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before:" + Arrays.toString(objects));
        // Object invoke = method.invoke(dynamicProxyApi, objects); // 使用反射机制
        Object invoke = methodProxy.invoke(dynamicProxyApi, objects); // 不推荐使用，否则死循环
        System.out.println("after:" + invoke);
        return invoke;
    }



}
